<template>
  <div class="app-container home">
    <!-- 婚恋平台统计概览 -->
    <div class="dating-statistics">
      <div class="statistics-header">
        <h2 class="title">
          <i class="el-icon-pie-chart"></i>
          婚恋平台数据统计
        </h2>
        <p class="subtitle">实时掌握平台运营数据，助力精准决策</p>
      </div>

      <!-- 统计卡片 -->
      <el-row :gutter="20" class="statistics-cards">
        <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
          <div class="stat-card total-members">
            <div class="stat-icon">
              <i class="el-icon-user"></i>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ totalMembers }}</div>
              <div class="stat-label">总会员数</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
          <div class="stat-card today-new">
            <div class="stat-icon">
              <i class="el-icon-plus"></i>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ todayNew }}</div>
              <div class="stat-label">今日新增</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
          <div class="stat-card male-ratio">
            <div class="stat-icon">
              <i class="el-icon-male"></i>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ maleRatio }}%</div>
              <div class="stat-label">男性占比</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
          <div class="stat-card matches-today">
            <div class="stat-icon">
              <i class="el-icon-connection"></i>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ matchesToday }}</div>
              <div class="stat-label">今日匹配</div>
            </div>
          </div>
        </el-col>
      </el-row>

      <!-- 图表区域 -->
      <el-row :gutter="20" class="charts-section">
        <!-- 人员增长趋势 -->
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <div class="chart-container">
            <DatingGrowthChart 
              :chart-data="growthChartData"
              height="400px"
            />
          </div>
        </el-col>
        
        <!-- 性别分布 -->
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <div class="chart-container">
            <DatingGenderChart 
              :chart-data="genderChartData"
              height="400px"
            />
          </div>
        </el-col>

        <!-- 年龄分布 -->
        <el-col :xs="24" :sm="24" :md="12" :lg="6" :xl="6">
          <div class="chart-container">
            <DatingAgeChart 
              :chart-data="ageChartData"
              height="400px"
            />
          </div>
        </el-col>
      </el-row>

      <!-- 收入分布 -->
      <el-row :gutter="20">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <div class="chart-container">
            <DatingIncomeChart 
              :chart-data="incomeChartData"
              height="350px"
            />
          </div>
        </el-col>
      </el-row>

      <!-- 刷新时间 -->
      <div class="refresh-info">
        <i class="el-icon-refresh"></i>
        最后更新时间：{{ lastUpdateTime }}
        <el-button type="text" size="mini" @click="refreshData" :loading="loading">
          <i class="el-icon-refresh"></i> 刷新数据
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import DatingGrowthChart from './dashboard/DatingGrowthChart'
import DatingGenderChart from './dashboard/DatingGenderChart'
import DatingAgeChart from './dashboard/DatingAgeChart'
import DatingIncomeChart from './dashboard/DatingIncomeChart'
import { getDailyMemberGrowth, getGenderDistribution, getAgeDistribution, getIncomeDistribution, getMemberStatistics } from '@/api/dating/member'

export default {
  name: "Index",
  components: {
    DatingGrowthChart,
    DatingGenderChart,
    DatingAgeChart,
    DatingIncomeChart
  },
  data() {
    return {
      // 版本号
      version: "3.9.0",
      // 加载状态
      loading: false,
      // 统计数据
      totalMembers: 0,
      todayNew: 0,
      maleRatio: 0,
      matchesToday: 0,
      lastUpdateTime: '',
      // 图表数据
      growthChartData: {
        dates: [],
        maleData: [],
        femaleData: [],
        totalData: []
      },
      genderChartData: {
        maleCount: 0,
        femaleCount: 0
      },
      ageChartData: {
        ageRanges: [],
        maleCounts: [],
        femaleCounts: []
      },
      incomeChartData: {
        incomeRanges: [],
        counts: []
      }
    }
  },
  mounted() {
    this.loadStatisticsData()
  },
  methods: {
    goTarget(href) {
      window.open(href, "_blank")
    },
    
    // 加载统计数据
    async loadStatisticsData() {
      this.loading = true
      try {
        // 并行加载所有统计数据
        const [statsResponse, growthResponse, genderResponse, ageResponse, incomeResponse] = await Promise.all([
          getMemberStatistics(),
          getDailyMemberGrowth(30),
          getGenderDistribution(),
          getAgeDistribution(),
          getIncomeDistribution()
        ])

        // 处理基础统计数据
        if (statsResponse.code === 200) {
          const stats = statsResponse.data
          this.totalMembers = stats.totalMembers || 0
          this.todayNew = stats.todayNew || 0
          this.maleRatio = stats.maleRatio ? stats.maleRatio.toFixed(1) : 0
          this.matchesToday = stats.matchesToday || 0
        }

        // 处理增长趋势数据
        if (growthResponse.code === 200) {
          const growthData = growthResponse.data
          this.growthChartData = {
            dates: growthData.dates || [],
            maleData: growthData.maleData || [],
            femaleData: growthData.femaleData || [],
            totalData: growthData.totalData || []
          }
        }

        // 处理性别分布数据
        if (genderResponse.code === 200) {
          const genderData = genderResponse.data
          this.genderChartData = {
            maleCount: genderData.maleCount || 0,
            femaleCount: genderData.femaleCount || 0
          }
        }

        // 处理年龄分布数据
        if (ageResponse.code === 200) {
          const ageData = ageResponse.data
          this.ageChartData = {
            ageRanges: ageData.ageRanges || [],
            maleCounts: ageData.maleCounts || [],
            femaleCounts: ageData.femaleCounts || []
          }
        }

        // 处理收入分布数据
        if (incomeResponse.code === 200) {
          const incomeData = incomeResponse.data
          this.incomeChartData = {
            incomeRanges: incomeData.incomeRanges || [],
            counts: incomeData.counts || []
          }
        }

        this.lastUpdateTime = this.formatDateTime(new Date())
      } catch (error) {
        console.error('加载统计数据失败:', error)
        this.$modal.msgError('加载统计数据失败，请稍后重试')
        
        // 设置默认数据以避免页面错误
        this.setDefaultData()
      } finally {
        this.loading = false
      }
    },

    // 设置默认数据
    setDefaultData() {
      this.growthChartData = {
        dates: ['昨天', '今天'],
        maleData: [0, 0],
        femaleData: [0, 0],
        totalData: [0, 0]
      }
      this.genderChartData = {
        maleCount: 0,
        femaleCount: 0
      }
      this.ageChartData = {
        ageRanges: ['18-25', '26-30', '31-35', '36-40', '40+'],
        maleCounts: [0, 0, 0, 0, 0],
        femaleCounts: [0, 0, 0, 0, 0]
      }
      this.incomeChartData = {
        incomeRanges: ['3k以下', '3k-5k', '5k-8k', '8k-12k', '12k-20k', '20k以上'],
        counts: [0, 0, 0, 0, 0, 0]
      }
    },

    // 刷新数据
    refreshData() {
      this.loadStatisticsData()
    },

    // 格式化日期时间
    formatDateTime(date) {
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    }
  }
}
</script>

<style scoped lang="scss">
.home {
  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #676a6c;
  overflow-x: hidden;

  .dating-statistics {
    padding: 20px;
    background: #f5f7fa;
    min-height: 100vh;

    .statistics-header {
      text-align: center;
      margin-bottom: 30px;
      
      .title {
        font-size: 28px;
        font-weight: 500;
        color: #2c3e50;
        margin: 0 0 10px 0;
        
        i {
          margin-right: 10px;
          color: #409eff;
        }
      }
      
      .subtitle {
        font-size: 14px;
        color: #909399;
        margin: 0;
      }
    }

    .statistics-cards {
      margin-bottom: 30px;
      
      .stat-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 12px;
        padding: 25px 20px;
        color: white;
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.15);
        transition: all 0.3s ease;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        
        &::before {
          content: '';
          position: absolute;
          top: -50%;
          left: -50%;
          width: 200%;
          height: 200%;
          background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
          transition: all 0.3s ease;
          opacity: 0;
        }
        
        &:hover {
          transform: translateY(-5px);
          box-shadow: 0 15px 35px rgba(102, 126, 234, 0.2);
          
          &::before {
            opacity: 1;
          }
        }

        &.today-new {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        &.total-members {
          background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        
        &.male-ratio {
          background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }
        
        &.matches-today {
          background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
        }
        
        .stat-icon {
          position: absolute;
          top: 20px;
          right: 20px;
          opacity: 0.3;
          
          i {
            font-size: 40px;
          }
        }
        
        .stat-content {
          position: relative;
          z-index: 1;
          
          .stat-number {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 8px;
            line-height: 1;
          }
          
          .stat-label {
            font-size: 14px;
            opacity: 0.9;
            font-weight: 500;
          }
        }
      }
    }

    .charts-section {
      margin-bottom: 30px;
    }

    .chart-container {
      background: white;
      border-radius: 12px;
      padding: 20px;
      box-shadow: 0 4px 20px rgba(0,0,0,0.08);
      transition: all 0.3s ease;
      margin-bottom: 20px;
      
      &:hover {
        box-shadow: 0 8px 30px rgba(0,0,0,0.12);
        transform: translateY(-2px);
      }
    }

    .refresh-info {
      text-align: center;
      padding: 20px;
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.05);
      color: #909399;
      font-size: 13px;
      
      i {
        margin-right: 5px;
      }
      
      .el-button {
        margin-left: 10px;
        color: #409eff;
        
        &:hover {
          color: #66b1ff;
        }
      }
    }
  }

  // 响应式设计
  @media (max-width: 768px) {
    .dating-statistics {
      padding: 15px;
      
      .statistics-header .title {
        font-size: 24px;
      }
      
      .statistics-cards .stat-card {
        padding: 20px 15px;
        margin-bottom: 15px;
        
        .stat-content .stat-number {
          font-size: 28px;
        }
        
        .stat-icon i {
          font-size: 35px;
        }
      }
      
      .chart-container {
        padding: 15px;
        margin-bottom: 15px;
      }
    }
  }

  @media (max-width: 480px) {
    .dating-statistics {
      padding: 10px;
      
      .statistics-header .title {
        font-size: 20px;
      }
      
      .statistics-cards .stat-card {
        padding: 15px 10px;
        
        .stat-content .stat-number {
          font-size: 24px;
        }
        
        .stat-icon i {
          font-size: 30px;
        }
      }
    }
  }

  // 保持原有样式（如果需要）
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
  
  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }
  
  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

